package com.atguigu.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * ClassName: CompletableFutureDemo1
 * Package: com.atguigu.ssyx
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-04
 * Time: 13:11
 */

//CompletableFuture演示学习
public class CompletableFutureDemo1 {

    //supplyAsync 带返回值 创建异步对象的方法
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService service = Executors.newFixedThreadPool(3);
        System.out.println("main begin...");
        //创建异步对象
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程: " + Thread.currentThread().getName());
            int value = 1024;
            System.out.println("value:" + value);
            return value;
        }, service);
        Integer integer = future.get();
        System.out.println("main over..." + integer);


    }



    //创建异步对象 是不带返回值的
    public static void main1(String[] args) {
        //创建一个线程池
        ExecutorService service = Executors.newFixedThreadPool(3);
        System.out.println("main begin...");
        //创建异步对象 是不带返回值的
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程: " + Thread.currentThread().getName());
            int result = 1024;
            System.out.println("result:" + result);
        }, service);
        System.out.println("main over...");
    }

}
